package com.spider.demo05proxy2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ProxyUtil {
    public static <T>T createProxy(T obj) {
        T proxy = (T) Proxy.newProxyInstance(
                obj.getClass().getClassLoader(),
                obj.getClass().getInterfaces(),
                new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        long start = System.currentTimeMillis();
                        Object result = method.invoke(obj, args);
                        long end = System.currentTimeMillis();
                        System.out.println("方法耗时：" + (end - start)/1000 + "秒");
                        return result;
                    }
                }
        );
        return proxy;
    }
}
